<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Money extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'money';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

    protected static function init()
    {
        Money::event('before_insert', function ($data) {
            //给用户增加余额
            $money = $data['money'];
            if ($data['type'] == 0) {
                User::where(['id' => $data['user_id']])->setInc('money', $money);
            } else {
                User::where(['id' => $data['user_id']])->setDec('money', $money);
            }
        });
    }
    

    







    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
